using System;
using System.Collections.Generic;

namespace QQZiFramework.Membership
{
    public abstract class CommonMemberShip : IMembership
    {
        public abstract string MessageAsyncPath { get; }
        public abstract Type UserType { get; }
        public abstract LoginUserStateType UserStateType { get; }


        /// <summary>
        /// 自定义生成自增标识
        /// </summary>
        public virtual object GetCustomIdentityValue(string tableName, string primaryKey, DBContext context)
        {
            string key = tableName + "_identity";
            long identity = context.GetRedisDatabase(DBContext.DBIdentity_Redis_Database_Index).IncrementRedisItem(key);
            if (identity < 2)
            {
                object scalar = context.ExecScalar("select max(" + primaryKey + ") from " + tableName);
                if (scalar == null || scalar == DBNull.Value) identity = 1;
                else identity = Convert.ToInt64(scalar);
                identity++;
                context.GetRedisDatabase(DBContext.DBIdentity_Redis_Database_Index).SetRedisItem(key, identity, DateTime.Now.AddDays(1));
            }
            return identity;
        }
        /// <summary>
        /// 创建业务层（服务层）实例
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public virtual IBusiness CreateServiceInstance(Type type, DBContext context)
        {
            IBusiness bussiness = (IBusiness)System.Activator.CreateInstance(type, false);
            bussiness.SetVisiter(context);
            return bussiness;
        }


        public virtual DBContext CreateDBContext()
        {
            System.Collections.Hashtable args = new System.Collections.Hashtable();
            return new DBContext(null, null, null, this, args);
        }

        public abstract bool IsDevelopment();


        public virtual string OnRedisKey(string key)
        {
            return key;
        }

        public virtual string OnCacheKey(string key)
        {
            return key;
        }
        public abstract string UserToCookie(BaseUser u);
        public abstract BaseUser GetRedisUser(int userId, DBContext context);
        public abstract BaseUser GetCookieUser(string cookieValue);
        public abstract TokenInfo SetUserLoginState(BaseUser userInfo, LoginUserStateType userStateType, DateTime Expires, LoginPlatfromId platForm, object extValues, DBContext context);
        public abstract BaseUser GetUserFrom(DBContext context);
        public abstract string GetRequestHeader(DBContext context, string name);
        public virtual void OnDBContextInit(DBContext context)
        {
        }
        public virtual string OnUrlPermission(string url)
        {
            return url;
        }

        public abstract T GetSettingValue<T>(DBContext context, string path, bool throwNoFoundError = false, T defaultVal = default);
        public abstract void LogMessage(string message, string moduleName, DBContext context);
        public abstract void LogException(Exception ex, string moduleName, DBContext context);
        public abstract void LogDebug(string message, string moduleName , DBContext context);
        
        public abstract void ClearCache(DBContext context);
        public abstract Dictionary<string, QueryEntityConfig> GetPermissionQueryMapping(DBContext context);
        public abstract TokenInfo GetTokenInfo(DBContext context);

        /// <summary>
        /// Context关闭（连接，事务）
        /// </summary>
        /// <param name="dBContext"></param>
        public virtual void OnDBContextClosed(DBContext context)
        {
        }

        /// <summary>
        /// context释放
        /// </summary>
        /// <param name="context"></param>
        public virtual void OnDBContextDisposed(DBContext context)
        {
        }

        public string GetNacosUrl(string nacosServiceName, DBContext mainContext)
        {
            throw new NotImplementedException();
        }

    }


}